package com.mission.game.components;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mission.game.R;
import com.mission.game.creategame.CreateMissionScreen;
import com.mission.game.creategame.MissionControlScreen;

public class RemoveAdapter extends ArrayAdapter<String> {
	Context _context;
	int _layoutResourceId;
	ArrayList<String> _items;

	public RemoveAdapter(Context context, int layoutResourceId,
			ArrayList<String> itemNames) {
		super(context, layoutResourceId, itemNames);
		_layoutResourceId = layoutResourceId;
		_context = context;
		_items = itemNames;
	}

	static class ItemHolder {
		ImageView crossIcon;
		TextView itemTxt;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ItemHolder holder = null;
		if (row == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = vi.inflate(_layoutResourceId, parent, false);

			holder = new ItemHolder();
			holder.crossIcon = (ImageView) row.findViewById(R.id.item_cross);
			holder.itemTxt = (TextView) row.findViewById(R.id.item_text);
			holder.crossIcon.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (_context instanceof MissionControlScreen) {
						((MissionControlScreen)_context).removeMissionItem(position);
					}
					else if (_context instanceof CreateMissionScreen) {
						((CreateMissionScreen)_context).removeTaskItem(position);
					}
				}
			});
			row.setTag(holder);
		} else {
			holder = (ItemHolder) row.getTag();
		}
		holder.itemTxt.setText(_items.get(position));
		return row;
	}
}